package org.gemstone.btn;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;
import java.util.ArrayList;

import javax.swing.SwingConstants;

import org.come.Frame.ZhuFrame;
import org.come.bean.SuitOperBean;
import org.come.entity.Goodstable;
import org.come.entity.PartJade;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.GsonUtil;
import org.gemstone.panel.GemstoneOperationMainFrame;
import org.gemstone.panel.GemstoneOperationMainPanel;

import com.tool.btn.MoBanBtn;
import com.tool.role.RoleData;
import com.updateNew.MyIsif;



public class GemstoneOperationBtn extends MoBanBtn {

    /**
     * 0 帮助按钮 1,2合成 3,4重铸 5,6鉴定(单数为未按下)跳转面板按钮 7合成按钮 8重铸按钮 9重选类型按钮 10鉴定按钮
     */
    int typeBtn;

    public GemstoneOperationBtn(String iconpath, int type, int typeBtn) {
        super(iconpath, type);
        // TODO Auto-generated constructor stub
        this.typeBtn = typeBtn;
    }

    public GemstoneOperationBtn(String iconpath, int type, Color[] colors, String text, Font font, int typeBtn) {
        super(iconpath, type, colors);
        this.typeBtn = typeBtn;
        setText(text);
        setFont(font);
        setHorizontalTextPosition(SwingConstants.CENTER);
        setVerticalTextPosition(SwingConstants.CENTER);
    }


    @Override
    public void chooseyes() {
        // TODO Auto-generated method stub

    }

    @Override
    public void chooseno() {
        // TODO Auto-generated method stub

    }

    @Override
    public void nochoose(MouseEvent e) {
        // TODO Auto-generated method stub
        GemstoneOperationMainPanel gemstoneOperationMainPanel = GemstoneOperationMainFrame
                .getGemstoneOperationMainFrame().getGemstoneOperationMainPanel();
        	try {
                switch (typeBtn) {
                case 2:
                    gemstoneOperationMainPanel.getSyntherisCheckBtn().setIcons(CutButtonImage.cuts("skin/8100226/newbutton/K54.png"));
                    gemstoneOperationMainPanel.getAppraisalCheckBtn().setIcons(CutButtonImage.cuts("skin/8100226/newbutton/K57.png"));
                    gemstoneOperationMainPanel.getRecastCheckBtn().setIcons(CutButtonImage.cuts("skin/8100226/newbutton/K55.png"));
                    gemstoneOperation(gemstoneOperationMainPanel.getTypePanel(), gemstoneOperationMainPanel);
                    gemstoneOperationMainPanel.setTypePanel(1);
                    gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel().getCardLayout().show(gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel(), "synthesis");
                    gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel().getGemstoneOperationAppraisalAndRecastPanel().changBtnIcon();
                    break;
                case 3:
                    break;
                case 4:
                    gemstoneOperationMainPanel.getSyntherisCheckBtn().setIcons(CutButtonImage.cuts("skin/8100226/newbutton/K53.png"));
                    gemstoneOperationMainPanel.getAppraisalCheckBtn().setIcons(CutButtonImage.cuts("skin/8100226/newbutton/K57.png"));
                    gemstoneOperationMainPanel.getRecastCheckBtn().setIcons(CutButtonImage.cuts("skin/8100226/newbutton/K56.png"));
                    gemstoneOperation(gemstoneOperationMainPanel.getTypePanel(), gemstoneOperationMainPanel);
                    gemstoneOperationMainPanel.setTypePanel(2);
                    gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel().getCardLayout().show(gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel(), "appraisalAndRecast");
                    gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel().getGemstoneOperationAppraisalAndRecastPanel().changBtnIcon();
                    break;
                case 5:
                    break;
                case 6:
                    gemstoneOperationMainPanel.getSyntherisCheckBtn().setIcons(CutButtonImage.cuts("skin/8100226/newbutton/K53.png"));
                    gemstoneOperationMainPanel.getAppraisalCheckBtn().setIcons(CutButtonImage.cuts("skin/8100226/newbutton/K58.png"));
                    gemstoneOperationMainPanel.getRecastCheckBtn().setIcons(CutButtonImage.cuts("skin/8100226/newbutton/K55.png"));
                    gemstoneOperation(gemstoneOperationMainPanel.getTypePanel(), gemstoneOperationMainPanel);
                    gemstoneOperationMainPanel.setTypePanel(3);
                    gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel().getCardLayout().show(gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel(), "appraisalAndRecast");
                    gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel().getGemstoneOperationAppraisalAndRecastPanel().changBtnIcon();
                    break;
                case 7:
                    upServerInfo(17, gemstoneOperationMainPanel);
                    break;
                case 8:
                    upServerInfo(18, gemstoneOperationMainPanel);
                    break;
                case 9:
                    gemstoneOperationMainPanel
                            .getGemstoneOperationMainCardPanel()
                            .getGemstoneOperationAppraisalAndRecastPanel()
                            .getGemstoneOperationRecastTypePanel()
                            .setVisible(
                                    !gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel()
                                            .getGemstoneOperationAppraisalAndRecastPanel()
                                            .getGemstoneOperationRecastTypePanel().isVisible());
                    break;
                case 10:
                    upServerInfo(19, gemstoneOperationMainPanel);
                    break;

                default:
                    break;
                }
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
    }

    public int getTypeBtn() {
        return typeBtn;
    }

    public void setTypeBtn(int typeBtn) {
        this.typeBtn = typeBtn;
    }

    public static void gemstoneOperation(int typePanel, GemstoneOperationMainPanel gemstoneOperationMainPanel) {
    		switch (typePanel) {
            case 1:
                for (int i = 0; i < gemstoneOperationMainPanel.getGoodsLabes().length; i++) {
                    gemstoneOperationMainPanel.getGoodsLabes()[i] = null;
                    gemstoneOperationMainPanel.changeIcon(null, i, gemstoneOperationMainPanel
                            .getGemstoneOperationMainCardPanel().getGemstoneOperationSynthesisPanel().getGemstoneNumber());
                }
                gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel().getGemstoneOperationSynthesisPanel()
                        .getSynthesisBtn().setBtn(-1);
                gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel().getGemstoneOperationSynthesisPanel()
                        .getSynthesisBtn().setIcon(CutButtonImage.getImage("skin/8100226/newbutton/aj1h.png", -1, -1));
                break;
            case 2:
                for (int i = 0; i < gemstoneOperationMainPanel.getGoodsLabes().length; i++) {
                    gemstoneOperationMainPanel.getGoodsLabes()[i] = null;
                    GemstoneOperationMainFrame
                            .getGemstoneOperationMainFrame()
                            .getGemstoneOperationMainPanel()
                            .changeIcon(
                                    null,
                                    i,
                                    gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel()
                                            .getGemstoneOperationAppraisalAndRecastPanel().getGemstoneNumber());
                }
                break;
            case 3:
                for (int i = 0; i < gemstoneOperationMainPanel.getGoodsLabes().length; i++) {
                    gemstoneOperationMainPanel.getGoodsLabes()[i] = null;
                    GemstoneOperationMainFrame
                            .getGemstoneOperationMainFrame()
                            .getGemstoneOperationMainPanel()
                            .changeIcon(
                                    null,
                                    i,
                                    gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel()
                                            .getGemstoneOperationAppraisalAndRecastPanel().getGemstoneNumber());
                }
                break;

            default:
                break;
            }

    }
    /**上传到服务器*/
    public void upServerInfo(int typePanel, GemstoneOperationMainPanel gemstoneOperationMainPanel) {
        SuitOperBean suitOperBean = new SuitOperBean();
        suitOperBean.setGoods(new ArrayList<BigDecimal>());
        if (RoleData.getRoleData().getLoginResult().getGold().compareTo(new BigDecimal("500000")) < 0) {
            ZhuFrame.getZhuJpanel().addPrompt2("金额不足");
            return;
        }
        for (int i = 0; i < gemstoneOperationMainPanel.getGoodsLabes().length; i++) {
            if (gemstoneOperationMainPanel.getGoodsLabes()[i] != null) {
                Goodstable goodstableBack = gemstoneOperationMainPanel.getGoodsLabes()[i];
                if (goodstableBack.getGoodlock() == 1) {
                    ZhuFrame.getZhuJpanel().addPrompt("该物品已被加锁");
                    return;
                }
                if (GoodsListFromServerUntil.isExist(goodstableBack)) {
                    return;
                }
                int sum = 1;
                for (int j = 0; j < suitOperBean.getGoods().size(); j++) {
                    if (goodstableBack.getRgid().compareTo(suitOperBean.getGoods().get(j)) == 0) {
                        sum++;
                    }
                }
                if (sum > goodstableBack.getUsetime()) {
                    ZhuFrame.getZhuJpanel().addPrompt2("请凑齐物品再来");
                    return;
                }
                suitOperBean.getGoods().add(goodstableBack.getRgid());
            }
        }
        suitOperBean.setType(typePanel);
        // 重铸选择的类型
        if (typePanel == 18) {
            Goodstable reelectGoods = gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel().getGemstoneOperationAppraisalAndRecastPanel().getReelectGoods();
            suitOperBean.setJade(new PartJade(reelectGoods.getType().intValue(), 0));
        }
        // 返回背包剩余格数
        int packNumber = GoodsListFromServerUntil.Surplussum("-1", "-1", 999);
        if (packNumber <= 0) {
            ZhuFrame.getZhuJpanel().addPrompt2("背包空间不足");
            return;
        }
//        for (int i = 0; i < gemstoneOperationMainPanel.getGoodsLabes().length; i++) {
//            Goodstable goodstableBack = gemstoneOperationMainPanel.getGoodsLabes()[i];
//            if (goodstableBack != null) {
//                goodstableBack.goodxh(1);
//                if (goodstableBack.getUsetime() <= 0) {
//                    GoodsListFromServerUntil.Deletebiaoid(goodstableBack.getRgid());
//                }
//            }
//        }
//        if (typePanel == 17) {
//            RoleData.getRoleData().getLoginResult().setGold(RoleData.getRoleData().getLoginResult().getGold().subtract(new BigDecimal("5000000")));
//        }
        String sendmes = Agreement.suitOperateAgreement(GsonUtil.getGsonUtil().getgson().toJson(suitOperBean));
        SendMessageUntil.toServer(sendmes);
        qk(gemstoneOperationMainPanel.getTypePanel(), gemstoneOperationMainPanel);
    }

    /**
     * 清空
     *
     * @param typeBtn
     */
    public void qk(int typeBtn, GemstoneOperationMainPanel gemstoneOperationMainPanel) {
        try {
            switch (typeBtn) {
                case 1:
                    gemstoneOperation(gemstoneOperationMainPanel.getTypePanel(), gemstoneOperationMainPanel);
                    gemstoneOperationMainPanel.setTypePanel(1);
                    break;
                case 2:
//                gemstoneOperationMainPanel.getSyntherisCheckBtn().setIcons(CutButtonImage.cuts("skin/8100226/button/6.png"));
//                gemstoneOperationMainPanel.getAppraisalCheckBtn().setIcons(CutButtonImage.cuts("skin/8100226/button/6.png"));
//                gemstoneOperationMainPanel.getRecastCheckBtn().setIcons(CutButtonImage.cuts("skin/8100226/button/5.png"));

                    gemstoneOperation(gemstoneOperationMainPanel.getTypePanel(), gemstoneOperationMainPanel);
                    gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel()
                            .getGemstoneOperationAppraisalAndRecastPanel().changBtnIcon();
                    break;
                case 3:
                    gemstoneOperation(gemstoneOperationMainPanel.getTypePanel(), gemstoneOperationMainPanel);
                    gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel()
                            .getGemstoneOperationAppraisalAndRecastPanel().changBtnIcon();
                    // gemstoneOperationMainPanel.getSyntherisCheckBtn().setIcons(CutButtonImage.cuts("skin/8100226/button/6.png"));
                    // gemstoneOperationMainPanel.getAppraisalCheckBtn().setIcons(CutButtonImage.cuts("skin/8100226/button/5.png"));
                    // gemstoneOperationMainPanel.getRecastCheckBtn().setIcons(CutButtonImage.cuts("skin/8100226/button/6.png"));

                    // gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel()
                    // .getGemstoneOperationAppraisalAndRecastPanel().getReacastGrayBtn().setVisible(true);
                    // gemstoneOperationMainPanel.getGemstoneOperationMainCardPanel()
                    // .getGemstoneOperationAppraisalAndRecastPanel().getReacastBtn().setVisible(false);
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
